<?php

namespace common\logics\order;

/**
 * 订单逻辑模型
 * 
 * @author emhome<emhome@163.com>
 * @since 2.0
 */
trait OrderDecisionTrait {

    /**
     * 是否已支付
     * @return bool
     */
    public function getIsPay() {
        if ($this->created_at > 0 && $this->pay_time >= $this->created_at && $this->trade_no) {
            return true;
        }
        return false;
    }

    /**
     * 是否已过期
     * @return bool
     */
    public function getIsExpired() {
        $allowStatus = [
            static::STATUS_DEFAULT,
            static::STATUS_ACTIVE,
        ];
        if (in_array($this->status, $allowStatus) && $this->countdown > 0) {
            return false;
        }
        return true;
    }

    /**
     * 是否有退款
     * @return bool
     */
    public function getHasRefund() {
        return !!$this->refund;
    }

    /**
     * 是否可以支付
     * @return bool
     */
    public function getCanPay() {
        return !$this->isPay && !$this->isExpired;
    }

    /**
     * 是否可以取消
     * @return bool
     */
    public function getCanCancel() {
        if ($this->isPay) {
            return false;
        }
        if ($this->isExpired) {
            return true;
        }
        return false;
    }

    /**
     * 是否可以删除
     * @return bool
     */
    public function getCanDelete() {
        $allowStatus = [
            static::STATUS_CANCEL,
            static::STATUS_CLOSE,
        ];
        if (in_array($this->status, $allowStatus)) {
            return true;
        }
        if ($this->isPay) {
            if ($this->status === static::STATUS_SUCCESS) {
                return true;
            }
        } else {
            if ($this->isExpired) {
                return true;
            }
        }
        return false;
    }

    /**
     * 是否可以自动出库
     * @return bool
     */
    public function getCanOutbound() {
        if (!$this->isPay) {
            return false;
        }
        $allowStatus = [
            static::STATUS_PAID,
            static::STATUS_SHIPPING,
        ];
        if (!in_array($this->status, $allowStatus)) {
            return false;
        }
        if ($this->outbound) {
            return false;
        }
        if (time() > $this->pay_time + 10) {
            return true;
        }
        return false;
    }

}
